//
//  JigSawMainMovieView.m
//  JigSawKiosk
//
//  Created by Stulberg on 10-10-17.
//  Copyright 2010 FruitonLabs. All rights reserved.
//

#import "JigSawMainMovieView.h"
//#import "defines.h"

@implementation JigSawMainMovieView

@synthesize rootLayer, flMovieLayer, flMovie, movieCompleteTimer ;

#pragma mark Initialization
- (id) initWithMovie:(NSRect)frame movie:(NSURL*)movie {	
	url  = movie ;//= [NSURL fileURLWithPath: movie];
	self = [super initWithFrame:frame];
    if (self) {
		
		[self setupLayers] ;
        // Initialization code here.
    }
    return self;
}


- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		
		[self setupLayers] ;
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // set any NSColor for filling
    [[NSColor clearColor] setFill];
    NSRectFill(dirtyRect);
}

#pragma mark delegate methods

- (void)setDelegate:(id)aDelegate {
	// not retained
	delegate = aDelegate;
}

- (void)delegatedMethod {
	[delegate mainMovieViewCallback:self] ;
}

- (void) movieCompleteTimerMethod:(NSTimer*) timer {
	[self delegatedMethod] ;
	if ([movieCompleteTimer isValid]) {
		[movieCompleteTimer invalidate] ;
		movieCompleteTimer = nil ;
	}
}

-(void) startMovieWithCallbackOnComplete:(NSURL*)movieName {
	
	NSError *error ;
	[flMovie release] ;
	flMovie = [[QTMovie movieWithURL:movieName error:&error] retain] ;
	
	[flMovie setAttribute:[NSNumber numberWithInt:1] forKey:QTMovieLoopsAttribute] ;
	
	float mWidth = [rootLayer bounds].size.width ;
	float mHeight = (mWidth*9)/16 ;
	
	if (flMovie) {
		if (flMovieLayer) {
			[flMovieLayer setMovie:flMovie] ;
			
			//CGRect aRect = CGRectMake(0, 0, mWidth, mHeight) ;
			//flMovieLayer.frame = aRect ;
			
		}
		else {
			// create movie layer
			flMovieLayer = [QTMovieLayer layerWithMovie:flMovie] ;
			// size layer
			
			CGRect aRect = CGRectMake(0, 0, mWidth, mHeight) ;
			flMovieLayer.frame = aRect ;
			
			flMovieLayer.position = CGPointMake([rootLayer bounds].size.width*0.5, [rootLayer bounds].size.height*0.5);
			// scale the movie layer with the container (it will resize with the window)
			//flMovieLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
			[rootLayer addSublayer:flMovieLayer] ;
			
		}
	}
	// start the movie playing
	//[flMovie play];	
}


- (void) movieStart {
	if (flMovie) {
		[flMovie gotoBeginning] ;
		[flMovie play] ;
		QTTime movieDur = [flMovie duration] ;
		float time = (float) movieDur.timeValue/movieDur.timeScale ;
		//if(kDebug) printf("\n This movie time= %f \n", time) ;
		
		if ([movieCompleteTimer isValid]) {
			[movieCompleteTimer invalidate] ;
			movieCompleteTimer = nil ;
			[movieCompleteTimer release] ;
		}
		movieCompleteTimer = [[NSTimer alloc] initWithFireDate: [NSDate dateWithTimeIntervalSinceNow:time]
													  interval: 0.1
														target: self
													  selector:@selector(movieCompleteTimerMethod:)
													  userInfo:nil repeats:NO];
		[[NSRunLoop currentRunLoop] addTimer:movieCompleteTimer forMode:NSDefaultRunLoopMode];
	}
}

- (void) movieStop {
		[flMovie stop]; 

}


#pragma mark setup
-(void) setupLayers {
	rootLayer = [CALayer layer] ;
	rootLayer.backgroundColor = CGColorGetConstantColor(kCGColorClear) ;
	rootLayer.bounds = CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height) ;
	[self setLayer:rootLayer] ;
	
	
}


#pragma mark cleanup
- (void) dealloc {
	
	[rootLayer release] ;
	[flMovieLayer release] ;
	[flMovie release] ;
	
	[super dealloc] ;
}

@end
